﻿#region License
// (c) Intergen.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using MediaWiki.Office.Model.Utilities;

namespace MediaWiki.Office.Model
{
  public class NumberingLevel
  {
    private NumberingDefinition _numberingDefinition;
    private int _level;
    private int? _start;
    private string _alignment;
    private bool? _restartNumber;
    private string _text;
    private NumberFormat _numberFormat;
    private ParagraphProperties _paragraphProperties;
    private RunProperties _runProperties;
    private string _styleId;
    private string _numberSuffixType;

    public string NumberSuffixType
    {
      get { return _numberSuffixType; }
      set { _numberSuffixType = value; }
    }

    public string StyleId
    {
      get { return _styleId; }
      set { _styleId = value; }
    }

    public RunProperties RunProperties
    {
      get { return _runProperties; }
      set { _runProperties = value; }
    }

    public ParagraphProperties ParagraphProperties
    {
      get { return _paragraphProperties; }
      set { _paragraphProperties = value; }
    }

    public NumberFormat NumberFormat
    {
      get { return _numberFormat; }
      set { _numberFormat = value; }
    }

    public string Text
    {
      get { return _text; }
      set { _text = value; }
    }

    public bool? RestartNumber
    {
      get { return _restartNumber; }
      set { _restartNumber = value; }
    }

    public string Alignment
    {
      get { return _alignment; }
      set { _alignment = value; }
    }

    public int? Start
    {
      get { return _start; }
      set { _start = value; }
    }

    public int Level
    {
      get { return _level; }
      set { _level = value; }
    }

    public NumberingLevel(NumberingDefinition numberingDefinition, XElement numberingLevelElement)
    {
      if (numberingDefinition == null)
        throw new ArgumentNullException("numberingDefinition");
      if (numberingLevelElement == null)
        throw new ArgumentNullException("numberingLevelElement");

      _numberingDefinition = numberingDefinition;

      XNamespace ns = Document.WordprocessingML;

      _level = (int)numberingLevelElement.Attribute(ns + "ilvl");
      _start = XmlUtils.GetValue<int>(numberingLevelElement.Element(ns + "start"));
      _alignment = XmlUtils.GetValue(numberingLevelElement.Element(ns + "lvlJc"));

      if (numberingLevelElement.Element(ns + "lvlPicBulletId") != null)
        throw new Exception("lvlPicBulletId not currently supported.");

      _restartNumber = XmlUtils.GetBoolean(numberingLevelElement.Element(ns + "lvlRestart"));
      _text = XmlUtils.GetValue(numberingLevelElement.Element(ns + "lvlText"));
      // default to decimal if no format is defined
      _numberFormat = XmlUtils.GetValue<NumberFormat>(numberingLevelElement.Element(ns + "numFmt")) ?? NumberFormat.Decimal;

      _paragraphProperties = new ParagraphProperties(numberingDefinition.Document, numberingLevelElement.Element(ns + "pPr"));
      _runProperties = new RunProperties(numberingDefinition.Document, numberingLevelElement.Element(ns + "rPr"));
      _styleId = XmlUtils.GetValue(numberingLevelElement.Element(ns + "pStyle"));

      _numberSuffixType = XmlUtils.GetValue(numberingLevelElement.Element(ns + "suff"));
    }
  }
}